package com.ruoyi.web.controller.approval;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.page.R;
import com.ruoyi.web.controller.email.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.approval.domain.ExamineAndApprove;
import com.ruoyi.approval.service.IExamineAndApproveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审批管理Controller
 *
 * @author ruoyi
 * @date 2023-03-13
 */
@RestController
@RequestMapping("/audit/approve")
public class ExamineAndApproveController extends BaseController
{
    @Autowired
    private IExamineAndApproveService examineAndApproveService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 缓存需要回滚的信息  TODO 更改为redis缓存技术
    static R r = new R();

    /**
     * 查询审批管理列表
     */
    @PreAuthorize("@ss.hasPermi('audit:approve:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamineAndApprove examineAndApprove)
    {
        startPage();
        List<ExamineAndApprove> list = examineAndApproveService.selectExamineAndApproveList(examineAndApprove);
        return getDataTable(list);
    }

    /**
     * 查询未审批管理列表
     */
    @PreAuthorize("@ss.hasPermi('audit:approve:list')")
    @GetMapping("/listTwo")
    public TableDataInfo list()
    {
        startPage();
        List<ExamineAndApprove> list = examineAndApproveService.selectExamineAndApproveListTwo();
        return getDataTable(list);
    }

    /**
     * 导出审批管理列表
     */
    @PreAuthorize("@ss.hasPermi('audit:approve:export')")
    @Log(title = "审批管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamineAndApprove examineAndApprove)
    {
        List<ExamineAndApprove> list = examineAndApproveService.selectExamineAndApproveList(examineAndApprove);
        ExcelUtil<ExamineAndApprove> util = new ExcelUtil<ExamineAndApprove>(ExamineAndApprove.class);
        util.exportExcel(response, list, "审批管理数据");
    }

    /**
     * 获取审批管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('audit:approve:query')")
    @GetMapping(value = "/{serialNumber}")
    public AjaxResult getInfo(@PathVariable("serialNumber") Integer serialNumber)
    {
        return success(examineAndApproveService.selectExamineAndApproveBySerialNumber(serialNumber));
    }

    /**
     * 新增审批管理
     */
    @PreAuthorize("@ss.hasPermi('audit:approve:add')")
    @Log(title = "审批管理", businessType = BusinessType.INSERT)
    @PostMapping
    public void add(@RequestBody ExamineAndApprove examineAndApprove)
    {
        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE,"item.ChengCheng",examineAndApprove);
//        return toAjax(examineAndApproveService.insertExamineAndApprove(examineAndApprove));
    }

    /**
     * 修改审批管理
     */
    @PreAuthorize("@ss.hasPermi('audit:approve:edit')")
    @Log(title = "审批管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamineAndApprove examineAndApprove)
    {
        return toAjax(examineAndApproveService.updateExamineAndApprove(examineAndApprove));
    }

    /**
     * 删除审批管理
     */
    @PreAuthorize("@ss.hasPermi('audit:approve:remove')")
    @Log(title = "审批管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{serialNumbers}")
    public AjaxResult remove(@PathVariable Integer[] serialNumbers)
    {
        for (Integer serialNumber : serialNumbers) {
            r.addList(serialNumber);
        }
        return toAjax(examineAndApproveService.deleteExamineAndApproveBySerialNumbers(serialNumbers));
    }

    /**
     * 回滚
     */
    @PreAuthorize("@ss.hasPermi('personnel:examineAndApprove:rollback')")
    @Log(title = "审批记录回滚", businessType = BusinessType.DELETE)
    @DeleteMapping
    public Boolean rollback()
    {
        List list = r.getList();

        if (list.size()==0){
            return false;
        }else {
            Object[] array = list.toArray();
            examineAndApproveService.rollbackEmployee(array);
            r.setList(new ArrayList<>());
            return true;
        }
    }

    /**
     * 同意审批管理
     */
    @PreAuthorize("@ss.hasPermi('audit:approve:edit')")
    @Log(title = "审批管理", businessType = BusinessType.UPDATE)
    @PostMapping("/okAudit")
    public AjaxResult okEdit(@RequestBody ExamineAndApprove examineAndApprove)
    {
        return toAjax(examineAndApproveService.updateOkExamineAndApprove(examineAndApprove));
    }

    /**
     * 拒绝审批管理
     */
    @PreAuthorize("@ss.hasPermi('audit:approve:edit')")
    @Log(title = "审批管理", businessType = BusinessType.UPDATE)
    @PostMapping("/noAudit")
    public AjaxResult noEdit(@RequestBody ExamineAndApprove examineAndApprove)
    {
        return toAjax(examineAndApproveService.updateNoExamineAndApprove(examineAndApprove));
    }
}
